package com.feemanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.annotation.Anonymous;
import com.common.core.domain.AjaxResult;
import com.common.core.domain.R;
import com.common.core.page.PageDomain;
import com.common.core.page.TableDataInfo;
import com.common.utils.StringUtils;
import com.common.validate.add;
import com.common.validate.edit;
import com.feemanage.domain.BizProjectOutboundMain;
import com.feemanage.domain.vo.BizProjectOutboundMainVo;
import com.feemanage.domain.vo.ProjectOutboundMainVO;
import com.feemanage.service.IBizProjectOutboundMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 经费外拨
 */
@Api(tags="经费外拨")
@RestController
@RequestMapping("/feemanage/outbound")
public class BizProjectOutboundMainController {

    @Autowired
    private IBizProjectOutboundMainService baseService;

    /**
     * 查询列表
     * @param pageDomain
     * @param main
     * @return
     */
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public TableDataInfo list(PageDomain pageDomain, BizProjectOutboundMain main){
        IPage<BizProjectOutboundMainVo> page = baseService.selectList(pageDomain,main);
        return TableDataInfo.build(page.getRecords(),page.getTotal());
    }

    /**
     * 检索表头统计
     * @param main
     * @return
     */
    @ApiOperation("检索表头统计")
    @GetMapping(value = "/listtotal")
    public AjaxResult listtotal(BizProjectOutboundMain main)
    {
        List<HashMap<String, Integer>> list =baseService.selectListTotal(main);
        return AjaxResult.success(list);
    }


    /**
     * 根据项目id获取信息
     * @param projectId
     * @return
     */
    @ApiOperation("根据项目id获取信息,新增时用,传项目id")
    @GetMapping("/getProOutFeeInfo")
    public R<ProjectOutboundMainVO> getProOutFeeInfo(@NotEmpty(message = "未选择项目！") String projectId){
        return R.ok(baseService.getProOutFeeInfo(projectId));
    }


    /**
     * 根据主表id获取编辑页面展示数据
     * @param Id
     * @return
     */
    @ApiOperation("根据主表id获取信息，编辑和查看时，传列表row.id")
    @GetMapping("/getEditInfo")
    public R<ProjectOutboundMainVO> getEditOutFeeInfo(@NotEmpty(message = "未选择数据！") String Id){
        return R.ok(baseService.getEditOutFeeInfo(Id));
    }

    /**
     * 新增
     * @param mainVO
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/add")
    public R<Void> addProOutFee(@Validated(add.class) @RequestBody ProjectOutboundMainVO mainVO){
        String msg = baseService.addProOutFee(mainVO);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 修改
     * @param mainVO
     * @return
     */
    @ApiOperation("修改")
    @PostMapping("/edit")
    public R<Void> editProOutFee(@Validated(edit.class) @RequestBody  ProjectOutboundMainVO mainVO){
        String msg = baseService.editProOutFee(mainVO);
        return StringUtils.isEmpty(msg) ? R.ok() : R.fail(msg);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove( @NotEmpty(message = "主键不能为空") @PathVariable String[] ids)
    {
        Boolean result = baseService.delProOutFee(Arrays.asList(ids),true);
        return result ? R.ok() : R.fail();
    }

}
